<template>
  <view>
    <view class="good_list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="gotoPageDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query:'',//请求的关键字
          cid:'',//分类id
          pagenum:1,//要请求的页码
          pagesize:10//要一次请求的多少条数据
        },
        goodsList:[],
        total:0,
        isloading:false,
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
   
    onLoad(options){
      
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // console.log(this.queryObj);
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(cb){
        this.isloading = true
        const {data:res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isloading = false
        cb && cb()
        if(res.meta.status !== 200) return uni.$showMsg()
         this.goodsList = [...this.goodsList,...res.message.goods]
         this.total = res.message.total
         
      },
      gotoPageDetail(item){
        // console.log(item);
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
    },
    onReachBottom(){
      // 判断数据是否已经加载完
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
      if(this.isloading) return 
       this.queryObj.pagenum += 1,
        this.getGoodsList()
     
    },
    /*
    下拉刷新事件
    */
    onPullDownRefresh(){
      // 1.重置关键数据
      this.queryObj.pagenum = 0,
      this.goodsList = [],
      this.total = 0,
      this.isloading = false,
      
      //2.重新请求数据
      this.getGoodsList(()=>{
          uni.stopPullDownRefresh()
          // console.log(23);
        })
    }
  }
</script>

<style lang="scss">

</style>
